package com.hgc.ch01.base;

/**
 * Created by 01367627 on 2019/7/5.
 */
public class SleepWaitLock {

    private static Object lock = new Object();

    private static class MyThread extends Thread{

        public MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println(getName() + " is running.");
            synchronized (lock){
                System.out.println(getName() + " get the lock!");
                try {
                    //Thread.sleep(3000);
                    lock.wait(3000);
                    System.out.println(getName() + " hahahah!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName() + " release the lock!");
            }
            System.out.println(getName() + " run over.");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new MyThread("t1");
        Thread t2 = new MyThread("t2");
        t1.start();
        t2.start();
    }
}
